#include "dev_pwm.h"

// 各定时器PWM输出初始化
// period: 计数周期
// frequency：PWM频率
#ifdef STM32_PWM_TIM3
// PA6: TIM3_CH1
// PA7: TIM3_CH2
// PB0: TIM3_CH3
// PB1: TIM3_CH4
void tim3_pwm_init(uint16 period, uint16 frequency)
{  
	GPIO_InitTypeDef GPIO_InitStructure;
	TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
	TIM_OCInitTypeDef  TIM_OCInitStructure;

    uint16 psc = SYSCLK_FREQ / period / frequency;
    uint16 arr = period;
    
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
 	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB, ENABLE);

	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; // 复用推挽输出
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOA, &GPIO_InitStructure);

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; // 复用推挽输出
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOB, &GPIO_InitStructure);
	
	TIM_TimeBaseStructure.TIM_Period = arr; // 设置在下一个更新事件装入活动的自动重装载寄存器周期的值
	TIM_TimeBaseStructure.TIM_Prescaler =psc; // 设置用来作为TIMx时钟频率除数的预分频值
	TIM_TimeBaseStructure.TIM_ClockDivision = 0; // 设置时钟分割:TDTS = Tck_tim
	TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; // TIM向上计数模式
	TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); // 根据TIM_TimeBaseInitStruct中指定的参数初始化TIMx的时间基数单位

	TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; // 选择定时器模式:TIM脉冲宽度调制模式2
	TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; // 比较输出使能
	TIM_OCInitStructure.TIM_Pulse = 0; // 设置待装入捕获比较寄存器的脉冲值

#ifdef COMPLEMENTARY_PWM // 通用定时器互补输出，CH1和CH2互补，CH3和CH4互补
	TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; // 输出极性:TIM输出比较极性高
	TIM_OC1Init(TIM3, &TIM_OCInitStructure); // 根据TIM_OCInitStruct中指定的参数初始化CH1
    TIM_OC3Init(TIM3, &TIM_OCInitStructure); // 根据TIM_OCInitStruct中指定的参数初始化CH3
    TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; // 输出极性:TIM输出比较极性低
	TIM_OC2Init(TIM3, &TIM_OCInitStructure); // 根据TIM_OCInitStruct中指定的参数初始化CH2
    TIM_OC4Init(TIM3, &TIM_OCInitStructure); // 根据TIM_OCInitStruct中指定的参数初始化CH4
#else
    TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; // 输出极性:TIM输出比较极性高
	TIM_OC1Init(TIM3, &TIM_OCInitStructure); // 根据TIM_OCInitStruct中指定的参数初始化CH1
    TIM_OC2Init(TIM3, &TIM_OCInitStructure); // 根据TIM_OCInitStruct中指定的参数初始化CH3
	TIM_OC3Init(TIM3, &TIM_OCInitStructure); // 根据TIM_OCInitStruct中指定的参数初始化CH2
    TIM_OC4Init(TIM3, &TIM_OCInitStructure); // 根据TIM_OCInitStruct中指定的参数初始化CH4
#endif

	TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable); // CH1预装载使能
    TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Enable); // CH2预装载使能
    TIM_OC3PreloadConfig(TIM3, TIM_OCPreload_Enable); // CH3预装载使能
    TIM_OC4PreloadConfig(TIM3, TIM_OCPreload_Enable); // CH4预装载使能
	
	TIM_ARRPreloadConfig(TIM3, ENABLE); // 使能TIMx在ARR上的预装载寄存器
	
	TIM_Cmd(TIM3, ENABLE); // 使能TIM3
}
#endif
#ifdef STM32_PWM_TIM4
// PB6: TIM4_CH1
// PB7: TIM4_CH2
// PB8: TIM4_CH3
// PB9: TIM4_CH4
void tim4_pwm_init(uint16 arr, uint16 psc)
{  
	GPIO_InitTypeDef GPIO_InitStructure;
	TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
	TIM_OCInitTypeDef  TIM_OCInitStructure;

	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
 	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB , ENABLE);

	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; // 复用推挽输出
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOB, &GPIO_InitStructure);
	
	TIM_TimeBaseStructure.TIM_Period = arr; // 设置在下一个更新事件装入活动的自动重装载寄存器周期的值
	TIM_TimeBaseStructure.TIM_Prescaler =psc; // 设置用来作为TIMx时钟频率除数的预分频值
	TIM_TimeBaseStructure.TIM_ClockDivision = 0; // 设置时钟分割:TDTS = Tck_tim
	TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; // TIM向上计数模式
	TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure); // 根据TIM_TimeBaseInitStruct中指定的参数初始化TIMx的时间基数单位

	TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; // 选择定时器模式:TIM脉冲宽度调制模式2
	TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; // 比较输出使能
	TIM_OCInitStructure.TIM_Pulse = 0; // 设置待装入捕获比较寄存器的脉冲值
    
	TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; // 输出极性:TIM输出比较极性高
	TIM_OC1Init(TIM4, &TIM_OCInitStructure); // 根据TIM_OCInitStruct中指定的参数初始化CH1
    TIM_OC3Init(TIM4, &TIM_OCInitStructure); // 根据TIM_OCInitStruct中指定的参数初始化CH3
    TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; // 输出极性:TIM输出比较极性低
	TIM_OC2Init(TIM4, &TIM_OCInitStructure); // 根据TIM_OCInitStruct中指定的参数初始化CH2
    TIM_OC4Init(TIM4, &TIM_OCInitStructure); // 根据TIM_OCInitStruct中指定的参数初始化CH4

	TIM_OC1PreloadConfig(TIM4, TIM_OCPreload_Enable); // CH1预装载使能
    TIM_OC2PreloadConfig(TIM4, TIM_OCPreload_Enable); // CH2预装载使能
    TIM_OC3PreloadConfig(TIM4, TIM_OCPreload_Enable); // CH3预装载使能
    TIM_OC4PreloadConfig(TIM4, TIM_OCPreload_Enable); // CH4预装载使能
	
	TIM_ARRPreloadConfig(TIM4, ENABLE); // 使能TIMx在ARR上的预装载寄存器
	
	TIM_Cmd(TIM4, ENABLE); // 使能TIM4
}
#endif

// 电机PWM初始化函数
// frequency：PWM频率
void pwm_init(uint16 period, uint16 frequency)
{
#ifdef STM32_PWM_TIM3
    tim3_pwm_init(period, frequency);
#endif
#ifdef STM32_PWM_TIM4
    tim4_pwm_init(period, frequency);
#endif
}

// 4路PWM输出
void pwm_output_4ch(uint16 compare1, uint16 compare2, uint16 compare3, uint16 compare4)
{
#ifdef STM32_PWM_TIM3
    TIM_SetCompare1(TIM3, compare1);
    TIM_SetCompare2(TIM3, compare1);
    TIM_SetCompare3(TIM3, compare3);
    TIM_SetCompare4(TIM3, compare4);
#endif
}
